# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=cmake
pkgname=("${_realname}" "${_realname}-emacs" "${_realname}-vim")
pkgver=3.20.0
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('i686' 'x86_64')
url="https://www.cmake.org/"
license=(MIT)
makedepends=('gcc'
             'emacs'
             'jsoncpp-devel'
             'libarchive-devel'
             'libbz2-devel'
             'libcurl-devel'
             'libexpat-devel'
             'liblzma-devel'
             'librhash-devel'
             'libutil-linux-devel'
             'libuv-devel'
             'libzstd-devel'
             'ncurses-devel'
             'zlib-devel')
depends=('gcc-libs'
         'jsoncpp'
         'libcurl'
         'libexpat'
         'libarchive'
         'librhash'
         'libutil-linux'
         'libuv'
         'ncurses'
         'pkg-config'
         'zlib')
options=('staticlibs' 'strip')
source=(https://www.cmake.org/files/v${pkgver%.*}/${pkgname}-${pkgver}.tar.gz
        0001-Support-MSYS.patch)
sha256sums=('9c06b2ddf7c337e31d8201f6ebcd3bba86a9a033976a9aee207fe0c6971f4755'
            '52d2c802d85a21ccf4da88668304d854e4975620b7f53800ef9671539083c862')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in $@
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i ${srcdir}/${_fname}
  done
}

del_file_exists() {
  for _fname in $@
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgname}-${pkgver}
  del_file_exists Modules/CPackMsys.cmake \
    Modules/FindMsys.cmake \
    Modules/Platform/MSYS-Clang-C.cmake \
    Modules/Platform/MSYS-Clang-CXX.cmake \
    Modules/Platform/MSYS-Determine-CXX.cmake \
    Modules/Platform/MSYS-GNU.cmake \
    Modules/Platform/MSYS.cmake \
    Modules/Platform/MSYS-CXX.cmake \
    Modules/Platform/MSYS-GNU-C.cmake \
    Modules/Platform/MSYS-GNU-CXX.cmake \
    Modules/Platform/MSYS-GNU-Fortran.cmake \
    Modules/Platform/MSYS-windres.cmake || true

  # Msysize
  apply_patch_with_msg \
       0001-Support-MSYS.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH}
  cd ${srcdir}/build-${CARCH}
  MSYSTEM=MSYS ${srcdir}/${_realname}-${pkgver}/configure \
    --prefix=/usr \
    --system-libs \
    --no-qt-gui \
    --parallel=$(nproc) \
    --mandir=share/man \
    --docdir=share/doc/${_realname} \
    -- -DCURSES_FORM_LIBRARY=/usr/lib/libformw.dll.a \
      -DCTEST_TEST_CTEST=ON \
      -DCMAKE_BUILD_TYPE=Release
  #Fix tests
  local  _gccver=$(gcc --version | grep ^gcc | sed 's/^.* //g')
  sed -e "s|4.9.2|${_gccver}|g" -e "s|4.8.2|${_gccver}|g" \
       -i ${srcdir}/build-${CARCH}/Tests/CMakeTests/ImplicitLinkInfoTest.cmake
  plain "Start building..."
  make
  make DESTDIR=${srcdir}/dest install
}

#Enable once we can get all tests to pass
#check() {
#  cd ${srcdir}/build-${CARCH}
#  make test
#}


package_cmake() {
  cd ${srcdir}/dest
  
  install -d  ${pkgdir}/usr/bin/
  install -d  ${pkgdir}/usr/share/aclocal
  install -d  ${pkgdir}/usr/share/${_realname}-${pkgver}
  
  cp -r usr/bin/* ${pkgdir}/usr/bin/
  cp -r usr/share/aclocal/* ${pkgdir}/usr/share/aclocal/
  cp -r usr/share/cmake-${pkgver}/* ${pkgdir}/usr/share/cmake-${pkgver}/

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/Copyright.txt \
    ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE
}

package_cmake-vim() {
  pkgdesc="A cross-platform open-source make system (vim mode)"
  depends=("cmake=${pkgver}" 'vim')
  cd ${srcdir}/dest
  install -d ${pkgdir}/usr/share

  cp -rf usr/share/vim ${pkgdir}/usr/share/
}

package_cmake-emacs() {
  pkgdesc="A cross-platform open-source make system (Emacs mode)"
  depends=(cmake=${pkgver} 'emacs')
  cd ${srcdir}/dest

  install -d ${pkgdir}/usr/share
  cp -rf usr/share/emacs ${pkgdir}/usr/share/

  /usr/bin/emacs -batch -f batch-byte-compile \
    ${pkgdir}/usr/share/emacs/site-lisp/cmake-mode.el
}

